﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using LogicalComponent.rules.grid;
using LogicalComponent.util;
using LogicalComponent.util.exception;

namespace TestLogicalComponent.rules.grid
{
    [TestClass]
    public class TestGridLogic
    {
        [TestMethod]
        public void TestConstructor()
        {
            GridLogic<DummyTile> empty = new GridLogic<DummyTile>(0, 0, new SimpleTileFactory<DummyTile>());
            GridLogic<DummyTile> longGrid = new GridLogic<DummyTile>(10, 0, new SimpleTileFactory<DummyTile>());
            GridLogic<DummyTile> wideGrid = new GridLogic<DummyTile>(0, 10, new SimpleTileFactory<DummyTile>());
            GridLogic<DummyTile> grid = new GridLogic<DummyTile>(10, 10, new SimpleTileFactory<DummyTile>());
        }

        [TestMethod]
        public void TestGetSetTile() 
        {
            int sizeToTest = 15;
            int length = 10;
            int width = 5;
            GridLogic<DummyTile> grid = new GridLogic<DummyTile>(length, width, new SimpleTileFactory<DummyTile>());
            for (int i = -5; i < sizeToTest; i++) 
            {
                for (int j = -5; j < sizeToTest; j++) 
                {
                    if (i >= length || j >= width || i < 0 || j < 0)
                    {
                        DummyTile tile = new DummyTile();
                        Assert.IsNull(grid.GetTile(i, j));
                        Assert.IsNull(grid.GetTile(new Coordinate(i, j)));
                        grid.SetTile(i, j, tile);
                        Assert.AreSame(grid.GetTile(i, j), tile);
                        grid.SetTile(new Coordinate(i, j), tile);
                        Assert.AreSame(grid.GetTile(new Coordinate(i, j)), tile);
                    }
                    else
                    {
                        Assert.IsNotNull(grid.GetTile(i, j));
                        Assert.IsNotNull(grid.GetTile(new Coordinate(i, j)));
                        DummyTile newTile1 = new DummyTile();
                        grid.SetTile(i, j, newTile1);
                        Assert.AreSame(grid.GetTile(i, j), newTile1);
                        DummyTile newTile2 = new DummyTile();
                        grid.SetTile(new Coordinate(i, j), newTile2);
                        Assert.AreSame(grid.GetTile(i, j), newTile2);
                    }
                }
            } 
        }

        [TestMethod]
        public void TestCreateDestroyTile()
        {
            int sizeToTest = 10;
            GridLogic<DummyTile> grid = new GridLogic<DummyTile>(new SimpleTileFactory<DummyTile>());
            for (int i = -5; i < sizeToTest; i++)
            {
                for (int j = -5; j < sizeToTest; j++)
                {
                    try
                    {
                        grid.DestroyTile(new Coordinate(i, j));
                        Assert.Fail();
                    }
                    catch (LogicalUserError error) { }
                    try
                    {
                        grid.DestroyTile(i, j);
                        Assert.Fail();
                    }
                    catch (LogicalUserError error) { }

                    grid.CreateTile(i, j);
                    Assert.IsNotNull(grid.GetTile(i, j));
                    grid.DestroyTile(i, j);
                    Assert.IsNull(grid.GetTile(i, j));
                    
                    grid.CreateTile(new Coordinate(i, j));
                    Assert.IsNotNull(grid.GetTile(new Coordinate(i, j)));
                    grid.DestroyTile(new Coordinate(i, j));
                    Assert.IsNull(grid.GetTile(new Coordinate(i, j)));
                }
            }
        }
    }
}
